package com.lwgk.basic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lwgk.R;
import com.lwgk.compinterface.OnMultiClickListener;
import com.lwgk.utils.NetConnectUtils;
import com.lwgk.utils.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/17
 * --------------------------------------------------------------------------
 * 描述：不含标题的几个fragment
 * 注意事项：需要自定义标题，仅仅提供几个界面的Ui切换
 */
public abstract class BasicFragment extends Fragment {

    private View                      mBasicFragmentView;
    private FrameLayout               mFragmentContentLayout;
    private LinearLayout.LayoutParams mLayoutParams;
    private View                      mNormalView;
    private RelativeLayout            mFragmentWaiting;
    private RelativeLayout            mFragmentNothing;
    private LinearLayout              mFragmentNoNet;
    private LinearLayout              mFragmentNoData;
    private SmartRefreshLayout        mFragmentSmartRefreshLayout;
    private TextView                  mFragmentNoDatadescribe;
    private Button                    mFragmentNoDataButton;
    private Button                    mFragmentNoNetButton;
    private ImageView                 mFragmentNoDataImage;
    //定义一个当前状态（默认正在加载中）
    private STATE                     mCurrentState = STATE.LOADING;
    //定义三个状态
    private enum STATE {
        LOADING,//正在加载中
        SUCCESS,//加载成功
        EMPTYDATE,//加载失败
        NONET,;//无网界面

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBasicFragmentView = inflater.inflate(R.layout.fragment_basic, null);
        mFragmentContentLayout = (FrameLayout) mBasicFragmentView.findViewById(R.id.basic_fragment_content_layout);


        mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        //引入布局
        mNormalView = inflater.inflate(setContentSuccessView(), null);
        //初始化成功的界面
        mFragmentContentLayout.addView(mNormalView, mLayoutParams);

        //等待中的界面
        mFragmentWaiting = (RelativeLayout) mBasicFragmentView.findViewById(R.id.basic_fragment_waiting);
        //什么都没有的界面，包含无数据无网界面
        mFragmentNothing = (RelativeLayout) mBasicFragmentView.findViewById(R.id.basic_fragment_no_data);

        //无网界面
        mFragmentNoNet = (LinearLayout) mBasicFragmentView.findViewById(R.id.basic_fragment_no_net);

        //无数据界面
        mFragmentNoData = (LinearLayout) mBasicFragmentView.findViewById(R.id.basic_fragment_empty_data);


        //初始化刷新加载器
        mFragmentSmartRefreshLayout = (SmartRefreshLayout) mBasicFragmentView.findViewById(R.id.fragment_refreshLayout);
        //无数据的描述
        mFragmentNoDatadescribe = (TextView) mBasicFragmentView.findViewById(R.id.basic_fragment_no_data_text);
        //无数据界面重新加载按钮
        mFragmentNoDataButton = (Button) mBasicFragmentView.findViewById(R.id.basic_fragment_no_data_button);
        //无数据界面的图片
        mFragmentNoDataImage = (ImageView) mBasicFragmentView.findViewById(R.id.basic_fragment_no_data_image);
        //无网界面重新加载
        mFragmentNoNetButton = (Button) mBasicFragmentView.findViewById(R.id.basic_fragment_no_net_button);

        mCurrentState = STATE.LOADING;
        //页面切换的方法
        changeView();

        //初始化view
        initView(mNormalView);

        //初始化数据
        initData();

        //刷新数据
        mFragmentSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(0);
                mCurrentState = STATE.LOADING;
                changeView();
                initData();
            }
        });

        //无网点击界面
        mFragmentNoDataButton.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                changeView();
                initData();
            }
        });

        //无数据点击界面
        mFragmentNoNetButton.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                changeView();
                initData();
            }
        });


        return mBasicFragmentView;
    }


    private void changeView() {
        mFragmentWaiting.setVisibility(View.GONE);
        mFragmentNothing.setVisibility(View.GONE);
        mFragmentContentLayout.setVisibility(View.GONE);
        //根据当前状态切换
        switch (mCurrentState) {
            case LOADING:
                mFragmentWaiting.setVisibility(View.VISIBLE);
                break;
            case SUCCESS:
                mFragmentContentLayout.setVisibility(View.VISIBLE);
                break;
            case EMPTYDATE:
                mFragmentNothing.setVisibility(View.VISIBLE);
                mFragmentNoData.setVisibility(View.VISIBLE);
                mFragmentNoNet.setVisibility(View.GONE);
                break;
            case NONET:
                mFragmentNothing.setVisibility(View.VISIBLE);
                mFragmentNoData.setVisibility(View.GONE);
                mFragmentNoNet.setVisibility(View.VISIBLE);
                break;
        }
    }
    /**
     * 自动切换Ui，网络请求完，需要调用该方法来切换Ui,再initData中使用，网络请求完成数据后，
     *
     * @param haveNet 当前是否有网无网传 false ，有网的时候传true
     * @param obj     ，请求失败后传null ，如果有数据就传一个部位空的对象
     */
    protected void autoChengeUi(boolean haveNet, final Object obj) {
        //如果是无网状态
        if (!haveNet) {
            mCurrentState = STATE.NONET;
            ToastUtils.showIntTost(R.string.net_error);
            changeView();
            return;
        } else {
            //如果是有网状态,子线程获取数据
            //获取网络数据
            mCurrentState = checkData(obj);
            //主线程切换Ui
            changeView();
        }
    }

    private STATE checkData(Object obj) {
        if (!NetConnectUtils.isNetConnected()) {
            return STATE.NONET;
        } else {
            if (obj == null) {//如果数据为空，则失败
                return STATE.EMPTYDATE;
            } else {

                if (obj instanceof List) {//如果返回的是数组
                    List list = (List) obj;
                    if (list.size() > 0) {
                        return STATE.SUCCESS;
                    } else {
                        return STATE.EMPTYDATE;
                    }
                } else {//如果是对象

                    return STATE.SUCCESS;
                }
            }
        }
    }

    /**
     *
     * @return 向外暴露接口提供无数据界面的文字描述
     */
    public TextView getFragmentNoDatadescribe() {
        return mFragmentNoDatadescribe;
    }

    /**
     *
     * @return 向外暴露接口，是否显示或者隐藏文字
     */
    public Button getFragmentNoDataButton() {
        return mFragmentNoDataButton;
    }

    /**
     *
     * @return 向外暴露接口，显示是否需要啊更换图片
     */
    public ImageView getFragmentNoDataImage() {
        return mFragmentNoDataImage;
    }

    /**
     * @return 初始化view界面
     */
    protected abstract int setContentSuccessView();

    /**
     * 初始化view
     */
    protected abstract void initView(View successView);

    /**
     * 初始化数据
     */
    protected abstract void initData();
}
